package com.carkeeper.subject.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.pojo.StoreFull;
import com.carkeeper.subject.service.StoreService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@Api(tags = "服务商主体")
@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired
    private StoreService storeService;

   /* *//**
     * 查询所有
     *
     * @return
     * @param page 当前页码
     * @param size 当前页条数
     *//*
    @GetMapping("/{page}/{size}")
    public Result findAll(@PathVariable("page") Integer page,@PathVariable("size") Integer size) {
        try {
            Page<Store> list = storeService.findAll(page, size);
            PageResult<Store> result = new PageResult<>(list.getTotal(), list.getResult());
            return new Result(true, StatusCode.OK, "查询成功", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }*/

    /**
     * 带条件分页查询
     *
     * @param map  查询条件
     * @return
     */
    @ApiOperation("查询所有服务商")
    @GetMapping("/findPage")
    public Result<PageResult> findPage(@RequestParam Map map) {
        try {
            Page<Store> storePage = storeService.findPage(map);
            PageResult pageResult = new PageResult(storePage.getTotal(), storePage.getResult());
            return new Result(true, StatusCode.OK, "查询成功", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 添加用户
     *
     * @param storeFull
     * @return
     */
    @ApiOperation("添加服务商")
    @PostMapping
    public Result add(@RequestBody StoreFull storeFull) {
        try {
            storeService.add(storeFull);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }

    /**
     * 修改
     *
     * @param storeFull
     * @return
     */
    @ApiOperation("修改服务商")
    @PutMapping
    public Result update(@RequestBody StoreFull storeFull) {
        try {
            storeService.update(storeFull);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "修改失败");
    }

    /**
     * 锁定/解锁
     *
     * @param id
     * @return
     */
    @ApiOperation("服务商的锁定/解锁")
    @GetMapping("/lockOrUnlock")
    private Result lockOrUnlock(String id) {
        try {
            storeService.lockOrUnlock(id);
            return new Result(true, StatusCode.OK, "改变成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "改变失败");
    }

    /**
     * 降序查询需要审核的商家
     * @return
     */
    @ApiOperation("降序查询需要审核的商家")
    @GetMapping("/findAudited")
    public Result findAudited(@RequestParam(value = "currPage",defaultValue = "1",required = false) Integer currPage,@RequestParam(value = "pageSize",defaultValue = "10",required = false) Integer pageSize) {
        try {
            if(currPage==null){
                currPage=1;
            }
            if(pageSize==null){
                pageSize=5;
            }
            Page<Store> list = storeService.findAudited(currPage,pageSize);
            return new Result(true, StatusCode.OK, "查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 查找被阻绝的商家
     * @return
     */
        @ApiOperation("查找被阻绝的商家")
    @GetMapping("/findrejected/{currPage}/{pageSize}")
    public Result findrejected(@PathVariable("currPage") Integer currPage,@PathVariable("pageSize") Integer pageSize) {
        try {
            if(currPage==null){
                currPage=1;
            }
            if(pageSize==null){
                pageSize=5;
            }
            Page<Store> list = storeService.findrejected(currPage,pageSize);
            return new Result(true, StatusCode.OK, "查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 根据条件分页查询被拒绝的商家
     * @param currPage
     * @param pageSize
     * @param map
     * @return
     */
    @ApiOperation("根据条件分页查询被拒绝的商家")
    @GetMapping("/findRejectedByCondition/{currPage}/{pageSize}")
    public Result findRejectedByCondition(@PathVariable("currPage") Integer currPage,@PathVariable("pageSize") Integer pageSize,@RequestParam Map<String,Object> map) {
        try {
            if(currPage==null){
                currPage=1;
            }
            if(pageSize==null){
                pageSize=5;
            }
            Page<Store> list = storeService.indRejectedByCondition(currPage,pageSize,map);
            return new Result(true, StatusCode.OK, "查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }


    /**
     * 根据条件分页查询要审核的商家
     * @param currPage
     * @param pageSize
     * @param map
     * @return
     */
    @ApiOperation("根据条件分页查询要审核的商家")
    @GetMapping("/findAuditedByCondition/{currPage}/{pageSize}")
    public Result findAuditedByCondition(@PathVariable("currPage") Integer currPage,@PathVariable("pageSize") Integer pageSize,@RequestParam Map<String,Object> map) {
        try {
            if(currPage==null){
                currPage=1;
            }
            if(pageSize==null){
                pageSize=5;
            }
            Page<Store> list = storeService.findAuditedByCondition(currPage,pageSize,map);
            return new Result(true, StatusCode.OK, "查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    @ApiOperation("查询商家详情")
    @GetMapping("/findStoreDetail/{code}")
    public Result findStoreDetail(@PathVariable("code") String code) {
        try {
            StoreFull storeFull = storeService.findStoreDetail(code);
            return new Result(true, StatusCode.OK, "查询成功",storeFull);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @ApiOperation("审核通过")
    @PutMapping("/pass/{id}")
    public Result pass(@PathVariable("id") Long id) {
        try {
            int count = storeService.pass(id);
            if(count>0){
                return new Result(true, StatusCode.OK, "审核成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "审核失败");
    }

    @ApiOperation("审核拒绝")
    @PutMapping("/reject/{id}")
    public Result reject(@PathVariable("id") Long id) {
        try {
            int count = storeService.reject(id);
            if(count>0){
                return new Result(true, StatusCode.OK, "拒绝成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "拒绝失败");
    }

    /**
     * 根据id查询服务商
      * @param id
     * @return
     */
    @ApiOperation("根据id查询服务商")
    @GetMapping("/findById/{id}")
    public Result<StoreFull> findById(@PathVariable("id") String id) {
        StoreFull storeFull = null;
        try {
            storeFull = storeService.findById(id);
            return new Result<>(true, StatusCode.OK, "查询成功", storeFull);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<>(false, StatusCode.ERROR, "查询失败");
    }


}
